/*
 * Copyright (c) 2010, Jim Hollinger
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Jim Hollinger nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifndef COMMON_PROPERTIES_H_
#define COMMON_PROPERTIES_H_

#include <stdio.h>
#include <string>
#include <vector>

namespace prop {

/**
 ****************************************************************************
 * @brief  A class to manage a list of key/value pairs (properties).
 *
 *  The list of key/value pairs can be initialized from a file
 *    and then queried via the getProperty method.
 *
 *  A typical use of this class is shown below.
 *   \code
 *    prop::Properties properties;
 *    properties.load("config.txt");
 *    std::string username = properties.getProperty("username", "my username");
 *    std::string priority = properties.getProperty("priority", "3");
 *    properties.listUnrecognized(stderr, "Unrecognized properties:\n");
 *   \endcode
 *  The above code will parse the following text file.
 *   \code
 *    # config.txt
 *    username  "John Doe"
 *    priority  1
 *   \endcode
 ****************************************************************************
**/
class Properties {
  protected:
    /**
     ****************************************************************************
     * @brief  A key/value pair.
     ****************************************************************************
    **/
    class Property {
        friend class Properties;

        public:
            Property(const std::string &key, const std::string &value);

        public:
            std::string key;        ///< The key of the key/value pair
            std::string value;      ///< The value of the key/value pair

        protected:
            bool recognized_flag;   ///< Set true when the key/value pair is retrieved
    };

  public:
    Properties();
    ~Properties();
    const std::string getProperty(const std::string &key);
    const std::string getProperty(const std::string &key, const std::string &defaultValue);
    bool          getBoolProperty(const std::string &key, const std::string &defaultValue);
    bool          getBoolProperty(const std::string &key, bool               defaultValue);
    int            getIntProperty(const std::string &key, const std::string &defaultValue);
    int            getIntProperty(const std::string &key, int                defaultValue);
    double       getFloatProperty(const std::string &key, const std::string &defaultValue);
    double       getFloatProperty(const std::string &key, double             defaultValue);
    bool list(const std::string &filename, const std::string &text);
    void list(FILE *fp, const std::string &text);
    bool listUnrecognized(FILE *fp, const std::string &text);
    bool load(const std::string &filename);
    void load(FILE *fp);
    static bool unitTest();

  protected:
    bool verifyProperty(const std::string &key, const std::string &expectedValue);
    void load(char *line, int len, int lineno);
    void add(const std::string &key, const std::string &value, bool recognized_flag = false);

  public:
    int         comment_char;   ///< Character indicating the start of a comment
    int         escape_char;    ///< Character indicating a two character escape sequence
    int         quote_char;     ///< Character indicating the begining or ending of a sequence of quoted characters

  protected:
    std::vector<Property *> properties;     ///< Internal list of key/value pairs
    std::string filename;                   ///< Copy of the filename loaded
};


}  // namespace prop

#endif  // COMMON_PROPERTIES_H_
